package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.PerformanceService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.performance.pojo.dto.PerformanceChartListDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceConfigListDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceItemDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceSaveAppDTO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceChartListVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceConfigListVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceItemVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceSaveVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 业绩目标
 * @Author: hongxiao
 * @date: 2019-01-24 21:24
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/performance")
public class MobilePerformanceController {

    @Resource
    private PerformanceService performanceService;

    /**
     * 获取业绩目标配置
     * @param performanceConfigListDTO 入参
     * @param br
     * @return 
     * 
     * @author hongxiao
     * @addTime: 2019-01-24 21:56
     * @since
     * 
     * @modified by: 
     * @updateTime: 2019-01-24 21:56
     * @version
     */
    @RequestMapping(value = "/configList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String configList(@RequestBody @Valid PerformanceConfigListDTO performanceConfigListDTO, BindingResult br) {
        XbbResponse<PerformanceConfigListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceConfigListVO performanceConfigListVO = performanceService.configListForMobile(performanceConfigListDTO);
                response = new XbbResponse<>(performanceConfigListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置目标初始化
     * @param performanceItemDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 21:28
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/itemApp", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String itemApp(@RequestBody @Valid PerformanceItemDTO performanceItemDTO, BindingResult br) {
        XbbResponse<PerformanceItemVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceItemVO performanceItemVO = performanceService.item(performanceItemDTO);
                response = new XbbResponse<>(performanceItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存业绩目标
     * @param performanceSaveAppDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-19 21:43
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/saveApp", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveApp(@RequestBody @Valid PerformanceSaveAppDTO performanceSaveAppDTO, BindingResult br) {
        XbbResponse<PerformanceSaveVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSaveVO performanceSaveVO = performanceService.saveApp(performanceSaveAppDTO);
                response = new XbbResponse<>(performanceSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 目标列表
     * @param performanceChartListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-19 11:13
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/chart/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String ChartList(@RequestBody @Valid PerformanceChartListDTO performanceChartListDTO, BindingResult br) {
        XbbResponse<PerformanceChartListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceChartListVO performanceListVO = performanceService.chartList(performanceChartListDTO);
                response = new XbbResponse<>(performanceListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
